﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Globalization;
using System.Web.Script.Serialization;

namespace OpenPhotoAlbum.Shared
{
    /// <summary>
    /// Summary description for Photos
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class Photos : AlbumWebService
    {
        [WebMethod]
        public string GetPhotos(Guid albumId)
        {
            if (albumId == null || albumId.Equals(Guid.Empty)) { return null; }

            List<PhotoDataItem> results = new List<PhotoDataItem>();

            IEnumerable<DbAccess.Photo> photos = BusinessLogics.PhotoManager.Instance.GetPhotos(albumId);
            if (photos == null || photos.Count() == 0) { return (new JavaScriptSerializer()).Serialize(results); }

            foreach (DbAccess.Photo photo in photos)
            {
                results.Add(new PhotoDataItem()
                {
                    Id = photo.PhotoId,
                    Caption = HttpUtility.HtmlEncode(photo.PhotoTitle),
                    UploadDate = string.Format(
                        CultureInfo.CurrentCulture,
                        Resources.Strings.My_UploadDate,
                        photo.PhotoCreationDate.ToLongDateString()),
                    PhotoPipeUrl = UrlContext.BuildPhotoPipeUrl(
                        photo.PhotoId, BusinessLogics.PhotoTypes.Thumbnail),
                    SharedPhotoUrl = UrlContext.BuildSharedPhotoUrl(photo.PhotoId)
                });
            }

            return (new JavaScriptSerializer()).Serialize(results);
        }
    }

    public class PhotoDataItem
    {
        public Guid Id { get; set; }
        public string Caption { get; set; }
        public string UploadDate { get; set; }
        public string PhotoPipeUrl { get; set; }
        public string SharedPhotoUrl { get; set; }
    }
}
